package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.mapper.CourseOrderMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "OrderConsumer",
        topic = PayConstants.TOPIC_PAY_ORDER_MESSAGE,
        selectorExpression = PayConstants.TAGS_PAY_ORDER_MESSAGE
)
public class OrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String data = new String(body, Constants.UTF8);
            JSONObject jsonObject = JSONObject.parseObject(data);
            CourseOrder courseOrder = new CourseOrder();
            courseOrder.setStatusOrder(RConst.Num.INT_1);
            courseOrder.setUpdateTime(new Date());
            courseOrderMapper.update(courseOrder,
                    new LambdaQueryWrapper<CourseOrder>()
                            .eq(CourseOrder::getOrderNo,jsonObject.get("orderNo")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
